/**
 * @file mg_child_device.c
 * @author Charry_Le (530673764@qq.com)
 * @brief 子设备管理源文件
 * @version 0.1
 * @date 2024-02-17
 * 
 * @copyright Moorgen Intelligent Technologies Copyright (c) 2024
 * 
 */
#include "mg_child_device.h"
#include "mg_Rs485Protocol.h"
#include "mg_device.h"

//地暖恢复出厂
void Heat_State_Factory_Reset(void)
{
	//地暖状态
	Heat.UserTemp = 20;//用户设置温度 		(存)
	Heat.AntiFreeze = OFF;//防冻
	MessageFlag[MSG_SAVE_CHILD_HEAT_STATE] = ON;
}
//温控器恢复出厂
void Air_State_Factory_Reset(void)
{
	//温控器参数
	Air.Mode = MODE_HEAT;						//模式
	Air.WindSpeed = WIND_SPEED_LOW;				//风速
	Air.UserTemp = 20;							//用户设置温度
	Air.VerWind = WIND_DIRECTION_VER_NONE;		//垂直风向
	Air.LevelWind = WIND_DIRECTION_VER_NONE;	//水平风向
	MessageFlag[MSG_SAVE_CHILD_AIR_STATE] = ON;
}
//新风恢复出厂
void Cdoas_State_Factory_Reset(void)
{
	//新风状态
	Cdoas.WindSpeed = CDOAS_WIND_SPEED_LOW;//风速
	Cdoas.PM25SetValu = 35;//设置值
	Cdoas.CO2SetValu = 400;//设置值
	Cdoas.HumSetValu = 60;//设置值
	Cdoas.Mode = CDOAS_MODE_NULL;
	MessageFlag[MSG_SAVE_CHILD_CDOAS_STATE] = ON;
}
//地暖恢复出厂
void Heat_Config_Factory_Reset(void)
{
	//地暖参数
	memset((uint8_t*)&Heat_Config.ConfigTable[1], 0xff, 254);
	Heat_Config.byte.TimerEn = OFF;			//定时使能
	Heat_Config.byte.Timer[0].Hour = 18;				//时
	Heat_Config.byte.Timer[0].Minute = 0;				//分
	Heat_Config.byte.Timer[1].Hour = 18;				//时
	Heat_Config.byte.Timer[1].Minute = 10;				//分
	Heat_Config.byte.TimeTempEn = OFF;					//四时间段编程使能
	for (int j = 0; j<TIME_TEMP_WEEK_MAX; j++)					//四时间段编程
	{
		Heat_Config.byte.TimeTemp[j].Term[0].Hour = 7;
		Heat_Config.byte.TimeTemp[j].Term[0].Min = 0;
		Heat_Config.byte.TimeTemp[j].Term[0].Temp = 18;

		Heat_Config.byte.TimeTemp[j].Term[1].Hour = 9;
		Heat_Config.byte.TimeTemp[j].Term[1].Min = 0;
		Heat_Config.byte.TimeTemp[j].Term[1].Temp = 18;

		Heat_Config.byte.TimeTemp[j].Term[2].Hour = 18;
		Heat_Config.byte.TimeTemp[j].Term[2].Min = 0;
		Heat_Config.byte.TimeTemp[j].Term[2].Temp = 18;

		Heat_Config.byte.TimeTemp[j].Term[3].Hour = 23;
		Heat_Config.byte.TimeTemp[j].Term[3].Min = 0;
		Heat_Config.byte.TimeTemp[j].Term[3].Temp = 5;
	}
	Heat_Config.byte.Heat_Temp_Range.Grads = Air_Config.byte.Heat_Temp_Range.Grads;
	Heat_Config.byte.Heat_Temp_Range.res = Air_Config.byte.Heat_Temp_Range.res;
	Heat_Config.byte.Heat_Temp_Range.Max = Air_Config.byte.Heat_Temp_Range.Max;
	Heat_Config.byte.Heat_Temp_Range.Min = Air_Config.byte.Heat_Temp_Range.Min;
	Heat_Config.byte.Switch_function.Switch_On_En = 1;
	Heat_Config.byte.Switch_function.Switch_Off_En = 1;
	Heat_Config.byte.Switch_function.Immobilization = 0;
	MessageFlag[MSG_SAVE_CHILD_HEAT_CONFIG] = ON;
}
//温控器恢复出厂
void Air_Config_Factory_Reset(void)
{
	//空调参数
	memset((uint8_t*)&Air_Config.ConfigTable[1], 0xff, 254);
	Air_Config.byte.Key_Mode = 0x01;
	memset((uint8_t*)&Air_Config.byte.Key_Func, 0, sizeof(Air_Config.byte.Key_Func));
	Air_Config.byte.TurnOn_Dispay = 0x01;
	Air_Config.byte.TurnOff_Dispay = 0x01;
	Air_Config.byte.TurnOn_Way = 0x02;
	Air_Config.byte.Light_Compensation_Plus = 0;
	Air_Config.byte.Light_Compensation_Minus = 0;
	Air_Config.byte.Custom_Wind = DEV_SPEED_NORMAL;//默认风速挡位
	Air_Config.byte.Custom_Mode = DEV_MODE_NORMAL;//默认模式挡位
	Air_Config.byte.Air_Temp_Range.Grads = 0;
	Air_Config.byte.Air_Temp_Range.res = 0;
	Air_Config.byte.Air_Temp_Range.Min = DEV_TEMP_MIN;//默认最低温度
	Air_Config.byte.Air_Temp_Range.Max = DEV_TEMP_MAX;//默认最高温度
	Air_Config.byte.TurnOff_Led_State = 0x01;//默认模式挡位
	Air_Config.byte.Chinese_Show = 1;
	Air_Config.byte.Relay_Diff.Plus = 1;	//默认继电器温差
	Air_Config.byte.Relay_Diff.Minus = 1;	//默认继电器温差
	Air_Config.byte.BackLight_Percent = 100;
	Air_Config.byte.Custom_Vertical_Dir = DEV_VERTICAL_WIND_NORMAL;
	Air_Config.byte.Custom_Level_Dir = DEV_LEVEL_WIND_NORMAL;
	Air_Config.byte.Heat_Temp_Range.Grads = 0;
	Air_Config.byte.Heat_Temp_Range.res = 0;			//默认最高温度
	Air_Config.byte.Heat_Temp_Range.Min = HEAT_TEMP_MIN;//默认最低温度
	Air_Config.byte.Heat_Temp_Range.Max = HEAT_TEMP_MAX;//默认最高温度
	Air_Config.byte.Low_Temp_Protection = 0;
	Air_Config.byte.Body_Sensing_Distance = 80;
	Air_Config.byte.Body_Light_Time = 60;
	Air_Config.byte.Seasonal_Pattern = 1;
	Air_Config.byte.Relay_Mode = 1;
	Air_Config.byte.Mode_function.Mode_Cool_En = 1;
	Air_Config.byte.Mode_function.Mode_Heat_En = 1;
	Air_Config.byte.Mode_function.Mode_Preheat_En = 1;
	Air_Config.byte.Mode_function.Mode_HeatPlus_En = 1;
	Air_Config.byte.Mode_function.Mode_HeatType_En = 1;
	Air_Config.byte.Mode_function.Immobilization = 0;
	Air_Config.byte.BackLight_DimmingOnTime = 0;
	Air_Config.byte.BackLight_DimmingOffTime = 50;
	Air_Config.byte.CurTemp_Source = 2;
	Air_Config.byte.Seasonal_Switch = OFF;
	Air_Config.byte.Summer_Start_Date.Mouth = 6;
	Air_Config.byte.Summer_Start_Date.Day = 1;
	Air_Config.byte.Summer_End_Date.Mouth = 9;
	Air_Config.byte.Summer_End_Date.Day = 1;
	Air_Config.byte.Winter_Start_Date.Mouth = 12;
	Air_Config.byte.Winter_Start_Date.Day = 1;
	Air_Config.byte.Winter_End_Date.Mouth = 3;
	Air_Config.byte.Winter_End_Date.Day = 1;
	Air_Config.byte.Summer_Air_Temp_Range.Max = DEV_TEMP_MAX;
	Air_Config.byte.Summer_Air_Temp_Range.Min = DEV_TEMP_MIN;
	Air_Config.byte.Winter_Air_Temp_Range.Max = DEV_TEMP_MAX;
	Air_Config.byte.Winter_Air_Temp_Range.Min = DEV_TEMP_MIN;
	MessageFlag[MSG_SAVE_CHILD_AIR_CONFIG] = ON;
}
//新风恢复出厂
void Cdoas_Config_Factory_Reset(void)
{
	//新风参数
	memset((uint8_t*)&Cdoas_Config.ConfigTable[1], 0xff, 254);
	Cdoas_Config.byte.Suppot = 0;	 	//类型
	Cdoas_Config.byte.Mode_Suppot = CDOAS_MODE_NORMAL;//支持的模式
	memset((uint8_t*)Cdoas_Config.byte.User_Mode1_Name, 0, sizeof(Cdoas_Config.byte.User_Mode1_Name));
	memset((uint8_t*)Cdoas_Config.byte.User_Mode2_Name, 0, sizeof(Cdoas_Config.byte.User_Mode2_Name));
	MessageFlag[MSG_SAVE_CHILD_CDOAS_CONFIG] = ON;
}
//设置地暖状态
void Set_Heat_State(uint8_t *Data, uint8_t DataLen)
{
	uint8_t m, i;
	float fTemp;

	printf("Moorgen INFO : Set_Heat_State Date: ");
	for (int i = 0; i < DataLen; i++)
	{
		printf("%02X ", Data[i]);
	}
	printf(", Data Len:%d\n", DataLen);
	if (Heat.OccupyState != ON)//非占用状态下，数据处理
	{
		if (Data[0] == CMD_HEAT_ON)//地暖开,CMD:0x01
		{
			if (Heat.Switch == ON)MessageFlag[MSG_HEAT_UPDATA_STAT] = ON;//状态上报
			Heat_Switch_Set(ON);
		}
		else if (Data[0] == CMD_HEAT_OFF)//地暖关,CMD:0x02
		{
			if (Heat.Switch == OFF)MessageFlag[MSG_HEAT_UPDATA_STAT] = ON;//状态上报
			Heat_Switch_Set(OFF);
		}
		else if (Data[0] == CMD_HEAT_ECO)// 地暖节能,CMD:0x03
		{
			Heat_ECO_Set();//节能模式温度
		}
		else if (Data[0] == CMD_HEAT_TIME_TEMP_EN)//四时间段使能,CMD:0x11
		{
			if (DataLen < 2)return;//长度判断
			if (Data[1] == 0)
				Heat_Four_Timer_Enable_Set(OFF);
			else
				Heat_Four_Timer_Enable_Set(ON);
		}
		else if (Data[0] == CMD_HEAT_SETTEMP)//温度设置
		{
			if (DataLen < 2)return;//长度判断
			fTemp = Data[1];//用户设置温度(显示)
			fTemp /= 2;
			Heat_Temp_Set(fTemp);
		}
		else if (Data[0] == CMD_HEAT_TEMP_GRADIENT_ADD)//温度梯度加,CMD:0x15
		{
			if (DataLen < 2)return;//长度判断
			fTemp = Data[1];
			if (Air_Config.byte.Heat_Temp_Range.Grads)
				fTemp *= 1;
			else
				fTemp *= 0.5;
			fTemp = Heat.UserTemp + fTemp;
			Heat_Temp_Set(fTemp);
		}
		else if (Data[0] == CMD_HEAT_TEMP_GRADIENT_DEL)//温度梯度减,CMD:0x16
		{
			if (DataLen < 2)return;//长度判断
			fTemp = Data[1];
			if (Air_Config.byte.Heat_Temp_Range.Grads)
				fTemp *= 1;
			else
				fTemp *= 0.5;
			fTemp = Heat.UserTemp - fTemp;
			Heat_Temp_Set(fTemp);
		}
		else if (Data[0] == CMD_HEAT_FUNCTION_DISABLE)//功能禁用
		{
			if (DataLen < 2)return;//长度判断
			if (Data[1] == 0x01)
				Set_Heat_Switch_Enable(ON, OFF);
			else if (Data[1] == 0x02)
				Set_Heat_Switch_Enable(OFF, OFF);
		}
		else if (Data[0] == CMD_HEAT_FUNCTION_ENABLE)//功能使能
		{
			if (DataLen < 2)return;//长度判断
			if (Data[1] == 0x01)
				Set_Heat_Switch_Enable(ON, ON);
			else if (Data[1] == 0x02)
				Set_Heat_Switch_Enable(OFF, ON);
		}
		else if (Data[0] == CMD_HEAT_SCENE_OPEN)//场景开,CMD:0x0f
		{
			if (DataLen < 2)return;//长度判断
			fTemp = Data[1];//用户设置温度(显示)
			fTemp /= 2;
			Heat_Temp_Set(fTemp);
			Heat_Switch_Set(ON);
		}
		else//数据错误
		{
			MessageFlag[MSG_HEAT_UPDATA_STAT] = ON;
		}
	}
	else//设备处于占用状态，保存控制数据
	{
		if (Data[0] == CMD_HEAT_ON)//地暖开
		{
			Heat_After_Occupy.Switch = ON;
		}
		else if (Data[0] == CMD_HEAT_OFF)//地暖关
		{
			Heat_After_Occupy.Switch = OFF;
		}
		else if (Data[0] == CMD_HEAT_SCENE_OPEN)//场景开
		{
			if (DataLen < 2)return;//长度判断
			fTemp = Data[1];
			fTemp /= 2;
			if (fTemp>(float)Air_Config.byte.Heat_Temp_Range.Max / 2)
				fTemp = (float)Air_Config.byte.Heat_Temp_Range.Max / 2;
			else if (fTemp<(float)Air_Config.byte.Heat_Temp_Range.Min / 2)
				fTemp = (float)Air_Config.byte.Heat_Temp_Range.Min / 2;
			Heat_After_Occupy.UserTemp = fTemp;
			Heat_After_Occupy.Switch = ON;
		}
		else if (Data[0] == CMD_HEAT_SETTEMP)
		{
			if (DataLen < 2)return;//长度判断
			fTemp = Data[1];
			fTemp /= 2;
			if (fTemp>(float)Air_Config.byte.Heat_Temp_Range.Max / 2)
				fTemp = (float)Air_Config.byte.Heat_Temp_Range.Max / 2;
			else if (fTemp<(float)Air_Config.byte.Heat_Temp_Range.Min / 2)
				fTemp = (float)Air_Config.byte.Heat_Temp_Range.Min / 2;
			Heat_After_Occupy.UserTemp = fTemp;
		}
		else if (Data[0] == CMD_HEAT_TEMP_GRADIENT_ADD)//温度梯度加
		{
			if (DataLen < 2)return;//长度判断
			fTemp = Data[1];
			if (Air_Config.byte.Heat_Temp_Range.Grads)
				fTemp *= 1;
			else
				fTemp *= 0.5;
			fTemp = Heat_After_Occupy.UserTemp + fTemp;
			if (fTemp>(float)Air_Config.byte.Heat_Temp_Range.Max / 2)
				fTemp = (float)Air_Config.byte.Heat_Temp_Range.Max / 2;
			else if (fTemp<(float)Air_Config.byte.Heat_Temp_Range.Min / 2)
				fTemp = (float)Air_Config.byte.Heat_Temp_Range.Min / 2;
			Heat_After_Occupy.UserTemp = fTemp;	//用户设置温度
		}
		else if (Data[0] == CMD_HEAT_TEMP_GRADIENT_DEL)//温度梯度减
		{
			if (DataLen < 2)return;//长度判断
			fTemp = Data[1];
			if (Air_Config.byte.Heat_Temp_Range.Grads)
				fTemp *= 1;
			else
				fTemp *= 0.5;
			fTemp = Heat_After_Occupy.UserTemp - fTemp;
			if (fTemp>(float)Air_Config.byte.Heat_Temp_Range.Max / 2)
				fTemp = (float)Air_Config.byte.Heat_Temp_Range.Max / 2;
			else if (fTemp<(float)Air_Config.byte.Heat_Temp_Range.Min / 2)
				fTemp = (float)Air_Config.byte.Heat_Temp_Range.Min / 2;
			Heat_After_Occupy.UserTemp = fTemp;	//用户设置温度
		}
		else if (Data[0] == CMD_HEAT_FUNCTION_DISABLE)//功能禁用
		{
			if (DataLen < 2)return;//长度判断
			if (Data[1] == 0x01)
				Set_Heat_Switch_Enable(ON, OFF);
			else if (Data[1] == 0x02)
				Set_Heat_Switch_Enable(OFF, OFF);
		}
		else if (Data[0] == CMD_HEAT_FUNCTION_ENABLE)//功能使能
		{
			if (DataLen < 2)return;//长度判断
			if (Data[1] == 0x01)
				Set_Heat_Switch_Enable(ON, ON);
			else if (Data[1] == 0x02)
				Set_Heat_Switch_Enable(OFF, ON);
		}
		MessageFlag[MSG_HEAT_UPDATA_STAT] = ON;//状态上报
	}
}
//设置温控器状态
void Set_Air_State(uint8_t *Data, uint8_t DataLen)
{
	uint16_t temp;
	float fTemp;

	printf("Moorgen INFO : Set_Air_State Date: ");
	for (int i = 0; i < DataLen; i++)
	{
		printf("%02X ", Data[i]);
	}
	printf(", Data Len:%d\n", DataLen);
	if (Air.OccupyState != ON)//设备没有占用
	{
		if (Data[0] == CMD_AIR_ON)//开
		{
			if (Air.Switch == ON)MessageFlag[MSG_AIR_UPDATA_STAT] = ON;//状态上报
			Air_Switch_Set(ON);
		}
		else if (Data[0] == CMD_AIR_OFF)//关
		{
			if (Air.Switch == OFF)MessageFlag[MSG_AIR_UPDATA_STAT] = ON;//状态上报
			Air_Switch_Set(OFF);
		}
		else if (Data[0] == CMD_AIR_SETFAN)//风速
		{
			if (DataLen < 2)return;//长度判断
			Air_Fan_Set((ENUM_AIR_WIND_SPEED)Data[1]);
		}
		else if (Data[0] == CMD_AIR_SETFANX)//风向
		{
			if (DataLen < 2)return;//长度判断
			Air_Vertical_FanDirection_Set((ENUM_AIR_WIND_DIRECTION_VER)((Data[1] >> 4) & 0x0F));
			Air_Level_FanDirection_Set((ENUM_AIR_WIND_DIRECTION_LEVEL)(Data[1] & 0x0F));
		}
		else if (Data[0] == CMD_AIR_SETMODE)//模式
		{
			if (DataLen < 2)return;//长度判断
			Air_Mode_Set((ENUM_AIR_MODE)Data[1]);
		}
		else if ((Data[0] == CMD_AIR_SETTEMP) ||//温度加
			(Data[0] == CMD_AIR_SETTEMP1))//温度减(用作温度设置)
		{
			if (DataLen < 2)return;//长度判断
			fTemp = Data[1];//用户设置温度(显示)
			fTemp /= 2;
			Air_Temp_Set(fTemp);
		}
		else if (Data[0] == CMD_AIR_TEMP_GRADIENT_ADD)//温度梯度加
		{
			if (DataLen < 2)return;//长度判断
			fTemp = Data[1];
			if (Air_Config.byte.Air_Temp_Range.Grads)
				fTemp *= 1;
			else
				fTemp *= 0.5;
			fTemp = Air.UserTemp + fTemp;
			Air_Temp_Set(fTemp);
		}
		else if (Data[0] == CMD_AIR_TEMP_GRADIENT_DEL)//温度梯度减
		{
			if (DataLen < 2)return;//长度判断
			fTemp = Data[1];
			if (Air_Config.byte.Air_Temp_Range.Grads)
				fTemp *= 1;
			else
				fTemp *= 0.5;
			fTemp = Air.UserTemp - fTemp;
			Air_Temp_Set(fTemp);
		}
		else if (Data[0] == CMD_AIR_SEASON_SET)//设置季节模式
		{
			if (DataLen < 2)return;//长度判断
			Set_Air_Season_Mode((ENUM_SEASON_MODE)Data[1]);
		}
		else if (Data[0] == CMD_AIR_FUNCTION_DISABLE)//功能禁能
		{
			if (DataLen < 2)return;//长度判断
			Set_Air_Mode_Enable((ENUM_AIR_MODE)Data[1], OFF);
		}
		else if (Data[0] == CMD_AIR_FUNCTION_ENABLE)//功能使能
		{
			if (DataLen < 2)return;//长度判断
			Set_Air_Mode_Enable((ENUM_AIR_MODE)Data[1], ON);
		}
		else if (Data[0] == CMD_AIR_SCNE_EXEC)//	场景中空调执行命令
		{
			if (DataLen < 4)return;//长度判断
			if (Data[1] == CMD_AIR_OFF)//关
			{
				Air_Switch_Set(OFF);
			}
			else if (Data[1] == CMD_AIR_ON)//开
			{
				//模式
				temp = Data[2] >> 4;
				Air_Mode_Set((ENUM_AIR_MODE)temp);
				//风速
				temp = Data[2] & 0x0f;
				Air_Fan_Set((ENUM_AIR_WIND_SPEED)temp);
				//温度
				fTemp = Data[3];//用户设置温度(显示)
				fTemp /= 2;
				Air_Temp_Set(fTemp);
				//风向
				if (DataLen >= 6)//存在风向控制命令
				{
					Air_Vertical_FanDirection_Set((ENUM_AIR_WIND_DIRECTION_VER)((Data[5] >> 4) & 0x0F));
					Air_Level_FanDirection_Set((ENUM_AIR_WIND_DIRECTION_LEVEL)(Data[5] & 0x0F));
				}
				//开机
				Air_Switch_Set(ON);
			}
		}
		else
		{
			MessageFlag[MSG_AIR_UPDATA_STAT] = ON;//状态上报
		}
	}
	else//设备处于占用状态，保存控制数据
	{
		if (Data[0] == CMD_AIR_ON)//开
		{
			Air_After_Occupy.Switch = ON;
		}
		else if (Data[0] == CMD_AIR_OFF)//关
		{
			Air_After_Occupy.Switch = OFF;
		}
		else if (Data[0] == (uint8_t)CMD_AIR_SETFAN)//占用风速
		{
			if (DataLen < 2)return;//长度判断
			if ((Air_After_Occupy.Mode == MODE_WIND) && ((ENUM_AIR_WIND_SPEED)Data[1] == WIND_SPEED_AUTO))
			{
			}
			else
			{
				if (((Air_Config.byte.Custom_Wind >> Data[1]) & 0x01) == 1)
					Air_After_Occupy.WindSpeed = (ENUM_AIR_WIND_SPEED)Data[1];
				if ((Air_After_Occupy.WindSpeed > WIND_SPEED_FFAST) || (Air_After_Occupy.WindSpeed < WIND_SPEED_LOW))
					Air_After_Occupy.WindSpeed = WIND_SPEED_AUTO;
			}
		}
		else if (Data[0] == (uint8_t)CMD_AIR_SETFANX)//占用风向
		{
			if (DataLen < 2)return;//长度判断
			if ((((Data[1] >> 4) & 0x0F) <= WIND_DIRECTION_VER_5) && ((Data[1] & 0x0F) <= WIND_DIRECTION_LEVEL_5))//有效值判断
			{
				if (((Air_Config.byte.Custom_Vertical_Dir >> (((Data[1] >> 4) & 0x0F) - 1)) & 0x01) == 1)//垂直风向
				{
					Air_After_Occupy.VerWind = (ENUM_AIR_WIND_DIRECTION_VER)((Data[1] >> 4) & 0x0F);
				}
				if (((Air_Config.byte.Custom_Level_Dir >> ((Data[1] & 0x0F) - 1)) & 0x01) == 1)//水平风向
				{
					Air_After_Occupy.LevelWind = (ENUM_AIR_WIND_DIRECTION_LEVEL)(Data[1] & 0x0F);
				}
			}
		}
		else if (Data[0] == (uint8_t)CMD_AIR_SETMODE)//占用模式
		{
			if (DataLen < 2)return;//长度判断
			if (((Air_Config.byte.Custom_Mode >> (Data[1] - 1)) & 0x01) == 1)
				Air_After_Occupy.Mode = (ENUM_AIR_MODE)Data[1];
			if ((Air_After_Occupy.Mode > MODE_HEAT_TYPE) || (Air_After_Occupy.Mode < MODE_COOL))
				Air_After_Occupy.Mode = MODE_COOL;
			if (Air_After_Occupy.Mode == MODE_WIND && Air_After_Occupy.WindSpeed == WIND_SPEED_AUTO)
				Air_After_Occupy.WindSpeed = WIND_SPEED_LOW;
		}

		else if ((Data[0] == CMD_AIR_SETTEMP) ||//占用温度加
			(Data[0] == CMD_AIR_SETTEMP1))//占用温度减
		{
			if (DataLen < 2)return;//长度判断
			fTemp = Data[1];//用户设置温度(显示)
			fTemp /= 2;
			if (fTemp>Get_Air_TempRange_Max())
			{
				fTemp = Get_Air_TempRange_Max();
			}
			else if (fTemp<Get_Air_TempRange_Min())
			{
				fTemp = Get_Air_TempRange_Min();
			}
			Air_After_Occupy.UserTemp = fTemp;	//用户设置温度
		}
		else if (Data[0] == CMD_AIR_TEMP_GRADIENT_ADD)//温度梯度加
		{
			if (DataLen < 2)return;//长度判断
			fTemp = Data[1];
			if (Air_Config.byte.Air_Temp_Range.Grads)
				fTemp *= 1;
			else
				fTemp *= 0.5;
			fTemp = Air_After_Occupy.UserTemp + fTemp;
			if (fTemp>Get_Air_TempRange_Max())
			{
				fTemp = Get_Air_TempRange_Max();
			}
			else if (fTemp<Get_Air_TempRange_Min())
			{
				fTemp = Get_Air_TempRange_Min();
			}
			Air_After_Occupy.UserTemp = fTemp;	//用户设置温度
		}
		else if (Data[0] == CMD_AIR_TEMP_GRADIENT_DEL)//温度梯度减
		{
			if (DataLen < 2)return;//长度判断
			fTemp = Data[1];
			if (Air_Config.byte.Air_Temp_Range.Grads)
				fTemp *= 1;
			else
				fTemp *= 0.5;
			fTemp = Air_After_Occupy.UserTemp - fTemp;
			if (fTemp>Get_Air_TempRange_Max())
			{
				fTemp = Get_Air_TempRange_Max();
			}
			else if (fTemp<Get_Air_TempRange_Min())
			{
				fTemp = Get_Air_TempRange_Min();
			}
			Air_After_Occupy.UserTemp = fTemp;	//用户设置温度
		}
		else if (Data[0] == CMD_AIR_SCNE_EXEC)//占用场景中空调执行命令
		{
			if (DataLen < 4)return;//长度判断
			if (Data[1] == CMD_AIR_OFF)//关
			{
				Air_After_Occupy.Switch = OFF;
			}
			else
			{
				//模式
				temp = Data[2];
				if ((temp >> 4) <= MODE_HEAT_TYPE && (temp >> 4) >= MODE_COOL)
				{
					if (((Air_Config.byte.Custom_Mode >> ((temp >> 4) - 1)) & 0x01) == 1)
					{
						Air_After_Occupy.Mode = (ENUM_AIR_MODE)(temp >> 4);
					}
				}
				else
					Air_After_Occupy.Mode = MODE_COOL;

				//风速
				temp = Data[2] & 0x0f;
				if (temp <= (uint16_t)WIND_SPEED_FFAST)
				{
					if (((Air_Config.byte.Custom_Wind >> temp) & 0x01) == 1)//排查风速是否使能
					{
						Air_After_Occupy.WindSpeed = (ENUM_AIR_WIND_SPEED)temp;
					}
				}
				else
					Air_After_Occupy.WindSpeed = WIND_SPEED_AUTO;
				if (Air_After_Occupy.Mode == MODE_WIND&&Air_After_Occupy.WindSpeed == WIND_SPEED_AUTO)
					Air_After_Occupy.WindSpeed = WIND_SPEED_LOW;

				//温度
				fTemp = Data[3];//用户设置温度(显示)
				fTemp /= 2;
				if (fTemp>Get_Air_TempRange_Max())
				{
					fTemp = Get_Air_TempRange_Max();
				}
				else if (fTemp<Get_Air_TempRange_Min())
				{
					fTemp = Get_Air_TempRange_Min();
				}
				Air_After_Occupy.UserTemp = fTemp;	//用户设置温度
				//风向
				if (DataLen >= 6)//存在背光控制命令
				{
					if ((((Data[5] >> 4) & 0x0F) <= WIND_DIRECTION_VER_5) && ((Data[5] & 0x0F) <= WIND_DIRECTION_LEVEL_5))
					{
						if (((Air_Config.byte.Custom_Vertical_Dir >> (((Data[5] >> 4) & 0x0F) - 1)) & 0x01) == 1)//垂直风向
						{
							Air_After_Occupy.VerWind = (ENUM_AIR_WIND_DIRECTION_VER)((Data[5] >> 4) & 0x0F);
						}
						if (((Air_Config.byte.Custom_Level_Dir >> ((Data[5] & 0x0F) - 1)) & 0x01) == 1)//水平风向
						{
							Air_After_Occupy.LevelWind = (ENUM_AIR_WIND_DIRECTION_LEVEL)(Data[5] & 0x0F);
						}
					}
				}
				//开关机
				if (Data[1] == CMD_AIR_ON)//开
				{
					Air_After_Occupy.Switch = ON;
				}
			}
		}
		MessageFlag[MSG_AIR_UPDATA_STAT] = ON;//状态上报
	}

}
//设置新风状态
void Set_Cdoas_State(uint8_t *Data, uint8_t DataLen)
{
	uint8_t m;
	uint16_t temp;

	printf("Moorgen INFO : Set_Cdoas_State Date: ");
	for (int i = 0; i < DataLen; i++)
	{
		printf("%02X ", Data[i]);
	}
	printf(", Data Len:%d\n", DataLen);
	if (Cdoas.OccupyState != ON)//设备没有占用
	{
		if (Data[0] == CMD_CDOAS_ON) //开
		{
			if (Cdoas.Switch == ON)MessageFlag[MSG_CDOAS_UPDATA_STAT] = ON;//状态上报
			Cdoas_Switch_Set(ON);
		}
		else if (Data[0] == CMD_CDOAS_OFF)//关
		{
			if (Cdoas.Switch == OFF)MessageFlag[MSG_CDOAS_UPDATA_STAT] = ON;//状态上报
			Cdoas_Switch_Set(OFF);
		}
		else if (Data[0] == CMD_CDOAS_SETCO2)//CO2/PM2.5浓度设置
		{
			if (DataLen < 3)return;//长度判断
			temp = Data[2];
			temp <<= 8;
			temp += Data[1];
			Cdoas_Value_Set(temp);
		}

		else if (Data[0] == CMD_CDOAS_SETFAN)//风速
		{
			if (DataLen < 2)return;//长度判断
			Cdoas_Fan_Set((ENUM_CDOAS_WIND_SPEED)Data[1]);
		}
		else if (Data[0] == CMD_CDOAS_SETMODE)//模式
		{
			if (DataLen < 2)return;//长度判断
			Cdoas_Mode_Set((ENUM_CDOAS_MODE)Data[1]);
		}
		else if (Data[0] == CMD_CDOAS_SCNE_EXEC)//场景中新风执行命令
		{
			if (DataLen < 4)return;//长度判断
			//开关
			m = Data[1];
			m >>= 4;
			if (m == 0x02)//关
			{
				Cdoas_Switch_Set(OFF);
			}
			else if (m == 0x01)//开
			{
				//CO2设置值
				temp = Data[3];
				temp <<= 8;
				temp += Data[2];

				Cdoas_Value_Set(temp);
				//风速
				m = Data[1] & 0x0f;
				Cdoas_Fan_Set((ENUM_CDOAS_WIND_SPEED)m);
				if (DataLen >= 6)//有模式字节
				{
					Cdoas.Mode = (ENUM_CDOAS_MODE)Data[5];
				}
				//开关
				Cdoas_Switch_Set(ON);
			}
		}
	}
	else
	{
		if (Data[0] == CMD_CDOAS_ON) //开
		{
			Cdoas_After_Occupy.Switch = ON;
		}
		else if (Data[0] == CMD_CDOAS_OFF)//关
		{
			Cdoas_After_Occupy.Switch = OFF;
		}
		else if (Data[0] == CMD_CDOAS_SETCO2)//占用CO2/PM2.5浓度设置
		{
			temp = Data[2];
			temp <<= 8;
			temp += Data[1];
			if (Cdoas_Config.byte.Suppot == 0x01)
			{
				if (temp > DEV_SET_CO2_MAX)
				{
					Cdoas_After_Occupy.CO2SetValu = DEV_SET_CO2_MAX;//设置值
				}
				else
				{
					Cdoas_After_Occupy.CO2SetValu = temp;//设置值
				}
			}
			else if (Cdoas_Config.byte.Suppot == 0x02)
			{
				if (temp > DEV_SET_PM25_MAX)
				{
					Cdoas_After_Occupy.PM25SetValu = DEV_SET_PM25_MAX;//设置值
				}
				else
				{
					Cdoas_After_Occupy.PM25SetValu = temp;//设置值
				}
			}
			else if (Cdoas_Config.byte.Suppot == 0x03)
			{
				if (temp > DEV_SET_HUM_MAX)
				{
					Cdoas_After_Occupy.HumSetValu = DEV_SET_HUM_MAX;//设置值
				}
				else
				{
					Cdoas_After_Occupy.HumSetValu = temp;//设置值
				}
			}
		}
		else if (Data[0] == CMD_CDOAS_SETFAN)//风速
		{
			Cdoas_After_Occupy.WindSpeed = (ENUM_CDOAS_WIND_SPEED)Data[1];
			if ((Cdoas_After_Occupy.WindSpeed > CDOAS_WIND_SPEED_HIGH) || (Cdoas_After_Occupy.WindSpeed < CDOAS_WIND_SPEED_LOW))
				Cdoas_After_Occupy.WindSpeed = CDOAS_WIND_SPEED_AUTO;
		}
		else if (Data[0] == CMD_CDOAS_SETMODE)//模式
		{
			Cdoas_After_Occupy.Mode = (ENUM_CDOAS_MODE)Data[1];
		}
		else if (Data[0] == CMD_CDOAS_SCNE_EXEC)//场景中新风执行命令
		{
			//开关
			m = Data[1];
			m >>= 4;
			if (m == 0x02)//关
			{
				Cdoas_After_Occupy.Switch = OFF;
			}
			else//开
			{
				//CO2设置值
				temp = Data[3];
				temp <<= 8;
				temp += Data[2];
				if (Cdoas_Config.byte.Suppot == 0x01)
				{
					if (temp > DEV_SET_CO2_MAX)
					{
						Cdoas_After_Occupy.CO2SetValu = DEV_SET_CO2_MAX;//设置值
					}
					else
					{
						Cdoas_After_Occupy.CO2SetValu = temp;//设置值
					}
				}
				else if (Cdoas_Config.byte.Suppot == 0x02)
				{
					if (temp > DEV_SET_PM25_MAX)
					{
						Cdoas_After_Occupy.PM25SetValu = DEV_SET_PM25_MAX;//设置值
					}
					else
					{
						Cdoas_After_Occupy.PM25SetValu = temp;//设置值
					}
				}
				else if (Cdoas_Config.byte.Suppot == 0x03)
				{
					if (temp > DEV_SET_PM25_MAX)
					{
						Cdoas_After_Occupy.PM25SetValu = DEV_SET_PM25_MAX;//设置值
					}
					else
					{
						Cdoas_After_Occupy.PM25SetValu = temp;//设置值
					}
				}

				//风速
				m = Data[1] & 0x0f;
				Cdoas_After_Occupy.WindSpeed = (ENUM_CDOAS_WIND_SPEED)m;
				if (Cdoas_After_Occupy.WindSpeed > CDOAS_WIND_SPEED_HIGH)
					Cdoas_After_Occupy.WindSpeed = CDOAS_WIND_SPEED_AUTO;
				//模式
				if (DataLen >= 6)//有模式字节
				{
					Cdoas.Mode = (ENUM_CDOAS_MODE)Data[5];
				}
				//开关
				m = Data[1];
				m >>= 4;
				if (m == 0x01)//开
				{
					Cdoas_After_Occupy.Switch = ON;
				}
			}
		}
		MessageFlag[MSG_CDOAS_UPDATA_STAT] = ON;//状态上报
	}
}